package org.example.trans;
import java.util.*;
import org.example.model.*;

/**
 * @author lenovo
 */

public class TransScore {
    //将ArrayList<ArrayList<String>>的第一行转为HashMap<String,Course>
    public static HashMap<String,Course> transScoreReadCourse
            (ArrayList<ArrayList<String>> arrayListOut){
        HashMap<String,Course> hashMap = new HashMap<>();
        if(arrayListOut == null){
            return null;
        }
        for(int i = 4 ; i < arrayListOut.get(0).size() ; i++){
            Course course = new Course();
            course.setCourseName(arrayListOut.get(0).get(i));
            hashMap.put(course.getCourseName(),course);
        }
        return hashMap;
    }
    //将ArrayList<ArrayList<String>>从第二行开始转为HashMap<String,Score>
    public static HashMap<String,Score> transScoreReadScore
            (ArrayList<ArrayList<String>> arrayListOut){
        HashMap<String,Score> hashMap = new HashMap<>();
        if(arrayListOut == null){
            return null;
        }
        for(int i = 1 ; i < arrayListOut.size() ; i++){
            ArrayList<String> arrayListIn = arrayListOut.get(i);
            Student student = new Student();
            Score score = new Score();
            HashMap<String,CourseScore> hashMapScore = new HashMap<>();
            student.setNum(arrayListIn.get(0));
            student.setName(arrayListIn.get(1));
            student.setGender(arrayListIn.get(2));
            student.setBirthday(arrayListIn.get(3));
            score.setStudent(student);
            for(int j = 4 ; j < arrayListIn.size() ; j++){
                try{
                    Course course = new Course();
                    CourseScore courseScore = new CourseScore();
                    course.setCourseName(arrayListOut.get(0).get(j));
                    courseScore.setCourse(course);
                    courseScore.setScore(Double.parseDouble(arrayListIn.get(j)));
                    hashMapScore.put(course.getCourseName(),courseScore);
                }catch(Exception e){
                    //
                }
            }
            score.setScore(hashMapScore);
            hashMap.put(student.getNum(),score);
        }
        return hashMap;
    }
    //将HashMap<String,Course>转为ArrayList<ArrayList<String>>的第一行
    //将HashMap<String,Score>转为ArrayList<ArrayList<String>>从第二行开始
    public static ArrayList<ArrayList<String>> transScoreWrite
            (HashMap<String,Course> hashMapCourse,HashMap<String,Score> hashMapScore){
        ArrayList<ArrayList<String>> arrayListOut = new ArrayList<>();
        if(hashMapCourse == null || hashMapScore == null){
            return null;
        }
        arrayListOut.add(new ArrayList<>());
        arrayListOut.get(0).add("学号");
        arrayListOut.get(0).add("姓名");
        arrayListOut.get(0).add("性别");
        arrayListOut.get(0).add("出生日期");
        for(Map.Entry<String,Course> map : hashMapCourse.entrySet()){
            arrayListOut.get(0).add(map.getKey());
        }
        for(Map.Entry<String,Score> map : hashMapScore.entrySet()){
            ArrayList<String> arrayListIn = new ArrayList<>();
            arrayListIn.add(map.getKey());
            arrayListIn.add(map.getValue().getStudent().getName());
            arrayListIn.add(map.getValue().getStudent().getGender());
            arrayListIn.add(map.getValue().getStudent().getBirthday());
            for(int i = 4 ; i < arrayListOut.get(0).size() ; i++){
                if(map.getValue().getScore().containsKey(arrayListOut.get(0).get(i))){
                    arrayListIn.add(String.format("%.2f",map.getValue().getScore().get(
                            arrayListOut.get(0).get(i)).getScore()));
                }else{
                    arrayListIn.add("--");
                }
            }
            arrayListOut.add(arrayListIn);
        }
        return arrayListOut;
    }
}
